package org.yangspring.samples.mvc.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.yangspring.samples.mvc.domain.AnimalJoin;

import java.util.List;

import static java.lang.System.out;

/**
 * Created by whyang on 2017/06/19.
 */

@Repository
public class AnimalDaoImpl implements AnimalDao {

    private static final Logger logger = LoggerFactory.getLogger(BooksDaoImpl.class);

    //    private LocalSessionFactoryBean sessionFactory;

    @Autowired
    @Qualifier("hibernate5AnnotatedSessionFactoryMysql")
    private SessionFactory sessionFactory;
    /*public void setSessionFactory(SessionFactory sessionFactory){
        this.sessionFactory = sessionFactory;
    }*/

    public void addAnimal(AnimalJoin animal){
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        session.persist(animal);
        logger.info("Aanimal saved successfully, Animal Details=" + animal);
    }

    public void updateAnimal(AnimalJoin animal){
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        session.update(animal);
        logger.info("Animal updated successfully, Animal Details=" + animal);
    }

    public List<AnimalJoin> listAnimal(){
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        List<AnimalJoin> animalsList = session.createQuery("from AnimalJoin").list();
        for(AnimalJoin b : animalsList){
            logger.info("\n\n\tAnimal List::" + b);
        }
        return animalsList;
//        return new ArrayList<AnimalJoin>();
    }

    public AnimalJoin getAnimalById(long id){
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        AnimalJoin a = (AnimalJoin) session.load(AnimalJoin.class, new Long(id));
        logger.info("Animal loaded successfully, Animal details=" + a);
        return a;

//        return new AnimalJoin();
    }

    public void removeAnimal(long id){
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        AnimalJoin a = (AnimalJoin) session.load(AnimalJoin.class, new Long(id));
        if(null != a){
            logger.info("\n\n\t a is Not Null\n\n");
            session.delete(a);
        }
        logger.info("Animal deleted successfully, Animal details=" + a);
    }

}
